package java12;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.GregorianCalendar;
import java.util.Scanner;

class Student {
	private int id;
	private String name;
	private int age;
	private double grade;
	
	public Student(){
		
	}
	public Student(int id, String name, int age, double grade) {
		this.id = id;
		this.setName(name);
		this.setAge(age);
		this.setGrade(grade);
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if (name.length()>10){
			throw new IllegalArgumentException("name's length should <=10 "+name.length());
		}
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if (age<=0){
			throw new IllegalArgumentException("age should >0 "+age);
		}
		this.age = age;
	}
	public double getGrade() {
		return grade;
	}
	public void setGrade(double grade) {
		if (grade<0 || grade >100){
			throw new IllegalArgumentException("grade should be in [0,100] "+grade);
		}
		this.grade = grade;
	}
	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
	}
	public void writeData(PrintWriter out)
	   {
	      
	      out.println(id + "|" + name + "|" + age + "|" + grade );
	   }

	   /**
	    * Reads employee data from a buffered reader
	    * @param in the scanner
	    */
	   public void readData(Scanner in)
	   {
	      String line = in.nextLine();
	      String[] tokens = line.split("\\|");
	      id =Integer.parseInt(tokens[0]);
	      name =tokens[1];
	      age = Integer.parseInt(tokens[2]);
	      grade = Double.parseDouble(tokens[3]);
	      
	      
	   }

}

public class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Student[] stu = new Student[3];

		stu[0] = new Student(01,"zheng", 20,98);
		stu[1] = new Student(02,"chen",19,80);
		stu[2] = new Student(03,"li",21,85);

	      try
	      {
	         // save all employee records to the file employee.dat
	         PrintWriter out = new PrintWriter("student.dat");
	         writeData(stu, out);
	         out.close();

	         // retrieve all records into a new array
	         Scanner in = new Scanner(new FileReader("student.dat"));
	         Student[] newStaff = readData(in);
	         in.close();

	         // print the newly read employee records
	         for (Student e : newStaff)
	            System.out.println(e);
	      }
	      catch (IOException exception)
	      {
	         exception.printStackTrace();
	      }
	   }
	private static void writeData(Student[] students, PrintWriter out) throws IOException
	   {
	      // write number of employees
	      out.println(students.length);

	      for (Student e : students)
	         e.writeData(out);
	   }

	   /**
	    * Reads an array of employees from a scanner
	    * @param in the scanner
	    * @return the array of employees
	    */
	   private static Student[] readData(Scanner in)
	   {
	      // retrieve the array size
	      int n = in.nextInt();
	      in.nextLine(); // consume newline

	      Student[] students = new Student[n];
	      for (int i = 0; i < n; i++)
	      {
	    	  students[i] = new Student();
	    	  students[i].readData(in);
	      }
	      return students;
	   }
}
